package com.wxandroid.guidedemo.activity;

import android.app.Fragment;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.wxandroid.guidedemo.MainActivity;
import com.wxandroid.guidedemo.R;
import com.wxandroid.guidedemo.adapter.CommonFragmentPagerAdapter;
import com.wxandroid.guidedemo.fragment.GuideFragment;

import java.util.LinkedList;


/**
 * Created by Administrator on 2016/12/16.
 */

public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnTouchListener {


    private ImageView iv_guide_down;
    private LinearLayout ll_scroll;
    private View v_scroll;


    private float lastOffset;
    private float lastX;
    private int pagerPosition;

    private int[] imgResUp;//上半部分图片
    private int[] imgResDown;//下半部分图片
    private AnimationDrawable animationDrawable = null;
    private LinkedList<Fragment> mFragmentList;
    private ViewPager vp_guide;


    @Override
    public void initView() {
        setInitTop(false);
        setContentView(R.layout.activity_guide);
        iv_guide_down = (ImageView) findViewById(R.id.iv_guide_down);
        ll_scroll = (LinearLayout) findViewById(R.id.ll_scroll);
        v_scroll = findViewById(R.id.v_scroll);
        vp_guide = (ViewPager) findViewById(R.id.vp_guide);

    }


    @Override
    public void fillDate() {
        imgResUp = new int[]{R.mipmap.guide_up_one_bg, R.mipmap.guide_up_two_bg, R.mipmap.guide_up_three_bg};
        imgResDown = new int[]{R.mipmap.guide_down_one, R.mipmap.guide_down_two, R.mipmap.guide_down_three};
        mFragmentList = new LinkedList<>();
        parseFragment();
        vp_guide.setAdapter(new CommonFragmentPagerAdapter(getFragmentManager(), mFragmentList));
        vp_guide.addOnPageChangeListener(this);
        vp_guide.setOnTouchListener(this);


    }


    private void parseFragment() {
        if (imgResUp != null) {
            int length = imgResUp.length;
            for (int i = 0; i < length; i++) {
                GuideFragment guideFragment = new GuideFragment();
                Bundle args = new Bundle();
                args.putInt("imgRes", imgResUp[i]);
                guideFragment.setArguments(args);
                mFragmentList.add(guideFragment);
            }
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) v_scroll.getLayoutParams();
        float distance = v_scroll.getWidth();
        int leftDistance = 0;
        if (lastOffset < positionOffset && positionOffset > 0) {
            //正向
            leftDistance = (int) (distance * (position));
            lp.leftMargin = leftDistance + (int) (positionOffset * distance);
            if (positionOffset < 0.5) {
                iv_guide_down.setImageResource(imgResDown[position]);
                float alpha = 1 - 2 * positionOffset;
                iv_guide_down.setAlpha(alpha < 0.2 ? 0.2f : alpha);
            } else {
                iv_guide_down.setImageResource(imgResDown[position + 1]);
                float alpha = (float) (2 * (positionOffset - 0.5));
                iv_guide_down.setAlpha(alpha < 0.2 ? 0.2f : alpha);
            }
        }
        if (lastOffset > positionOffset && positionOffset > 0) {
            //反向
            leftDistance = (int) (distance * (position + 1));
            lp.leftMargin = leftDistance +
                    (int) ((positionOffset - 1) * distance);
            if (positionOffset < 0.5) {
                iv_guide_down.setImageResource(imgResDown[position]);
                float alpha = 1 - 2 * positionOffset;
                iv_guide_down.setAlpha(alpha < 0.2 ? 0.2f : alpha);
            } else {
                iv_guide_down.setImageResource(imgResDown[position + 1]);
                float alpha = 2 * (positionOffset - 0.5f);
                iv_guide_down.setAlpha(alpha < 0.2 ? 0.2f : alpha);
            }
        }
//        if (lastOffset > positionOffset && positionOffset > 0) {
//            //反向
//            leftDistance = (int) (distance * (position + 1));
//            lp.leftMargin = leftDistance +
//                    (int) ((positionOffset - 1) * distance);
//            if (positionOffset < 0.5) {
//                iv_guide_down.setImageResource(imgResDown[position]);
//                float alpha = 1 - 2 * positionOffset;
//                iv_guide_down.setAlpha(alpha < 0.2 ? 0.2f : alpha);
//            } else {
//                iv_guide_down.setImageResource(imgResDown[position+1]);
//                float alpha = 2 * (positionOffset - 0.5f);
//                iv_guide_down.setAlpha(alpha < 0.2 ? 0.2f : alpha);
//            }
//        }

        lastOffset = positionOffset;
        lp.width = (int) distance;
        v_scroll.setLayoutParams(lp);
        //设置底部图片的透明图
        pagerPosition = position;

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (pagerPosition == 2) {
            int action = motionEvent.getAction();

            if (action == MotionEvent.ACTION_DOWN) {
                lastX = motionEvent.getX();
            } else if (action == MotionEvent.ACTION_MOVE) {
                if (lastX - motionEvent.getX() > 100) {
                    Intent intent = new Intent(this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                }
            }

        }

        return false;
    }

}
